package com.live.shop.scene.tool;

import android.content.Context;

import com.live.shop.model.KeywordModel;
import com.live.shop.model.MusicModel;
import com.live.shop.model.ProductGroupModel;
import com.live.shop.model.PrologueModel;
import com.live.shop.model.UserModel;
import com.live.shop.sql.KeywordListDatabase;
import com.live.shop.sql.MusicListDatabase;
import com.live.shop.sql.ProductGroupListDatabase;
import com.live.shop.sql.PrologueListDatabase;
import com.live.shop.sql.UserDatabase;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class ServerDataTool {

    //【服务端】获取所有数据：话术、关键词、背景音乐、产品组 进行和客户端同步
    public static JSONObject getAllData(Context context){
        JSONObject reJson = new JSONObject();
        try {
            KeywordListDatabase keyDb= new KeywordListDatabase(context);
            PrologueListDatabase proDb= new PrologueListDatabase(context);
            MusicListDatabase musicDb= new MusicListDatabase(context);
            ProductGroupListDatabase groupDb= new ProductGroupListDatabase(context);
            UserDatabase userDb = new UserDatabase(context);

            UserModel model = userDb.select();
            List<KeywordModel> keyList = keyDb.selectAllStatusYes();
            List<PrologueModel> proList = proDb.selectAllStatusYes();
            List<MusicModel> musicList = musicDb.selectAll();
            List<ProductGroupModel> groupList = groupDb.selectAllStatusYes();

            reJson.put("user",UserModel.model2Json(model));

            JSONArray keyArray = new JSONArray();
            for (KeywordModel m:keyList) {
                keyArray.put(KeywordModel.model2Json(m));
            }
            reJson.put("keyword_array",keyArray);

            JSONArray proArray = new JSONArray();
            for (PrologueModel m:proList) {
                proArray.put(PrologueModel.model2Json(m));
            }
            reJson.put("prologue_array",proArray);

            JSONArray musicArray = new JSONArray();
            for (MusicModel m:musicList) {
                musicArray.put(MusicModel.model2Json(m));
            }
            reJson.put("music_array",musicArray);

            JSONArray groupArray = new JSONArray();
            for (ProductGroupModel m:groupList) {
                groupArray.put(ProductGroupModel.model2Json(m));
            }
            reJson.put("group_array",groupArray);

        }catch (Exception e){
            e.printStackTrace();
        }
        return reJson;
    }
}
